#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
#Leo Zhang@2008
#

from google.appengine.ext import db
from google.appengine.ext.db import djangoforms

from django import http
from django.shortcuts import render_to_response
from django.core.paginator import ObjectPaginator
#import Models
from appModels.base_model import Feedback


def leaveWord(request, page=1):
  """
  deal with the visitor feedback
  """
##  GQL way to get data
##  feedbacks = db.GqlQuery("SELECT * "
##                          "FROM Feedback "
##                          "ORDER BY date DESC LIMIT 10")
  if request.method == 'GET':
    feedbacks = Feedback.all().order('-date')
    paginator = ObjectPaginator(feedbacks, 7)
    page = int(page)
    fdlist = paginator.get_page(page - 1)

    template_values = {
      'paginator': paginator,
      'is_paginated': paginator.pages > 1,
      'has_next': paginator.has_next_page(page - 1),
      'has_previous': paginator.has_previous_page(page - 1),
      'current_page': page,
      'next_page': page + 1,
      'previous_page': page - 1,
      'pages': paginator.pages,
      'hits' : paginator.hits,
      'feedbacks': fdlist,
      'site_address':'http://zol.appspot.com',
      }
    return render_to_response('feedback.html', template_values)
  
  if request.method == 'POST':
    content = request.POST['content'].decode('utf8')

    if request.POST['name']:
      name = request.POST['name'].decode('utf8')
    else:
      name = u'匿名����'
      
    feedback = Feedback(name=name, content=content)
    feedback.put()
    return http.HttpResponseRedirect('/guestbook')



